/* eslint-disable */
import Vue from 'vue'
import timestamp from 'time-stamp'
import { imageFill, imageLfit, imageMfit, videoFrame} from '@/utils/filters/oss'
import { Modal } from 'ant-design-vue'
import WImage from '../components/global/WImage'

declare module 'vue/types/vue' {
  interface Vue {
    $bgImage: (url: string, data: object) => object
    $wAssign: (org: object, data: object) => object
    $wRemovePop: (deleteFunc: () => Promise<any>, tip?: string, dustbin?: boolean) => Promise<any>
  }
}

Vue.prototype.$bgImage = (url: string, data = {}) => {
  const retDara = {
    backgroundImage: url ? `url(${url})` : undefined,
    ...data
  }
  return retDara
}

/**
 * 对象赋值操作，只赋值对象有的属性
 */
Vue.prototype.$wAssign = (org: object, data: object) => {
  for (const key in data) {
    // @ts-ignore
    if (org[key] !== undefined) {
      // @ts-ignore
      org[key] = data[key]
    }
  }
}

/**
 * 删除操作封装
 * @param <Promiss> deleteFunc  删除的异步操作
 * @param tip 删除提示
 * @param dustbin 是否添加到回收站
 */
Vue.prototype.$wRemovePop = async function(deleteFunc: () => Promise<any>, tip = '', dustbin = false) {
  return new Promise((resolve, reject) => {
    const tipStr = dustbin ? `确定要${tip ? '将' : ''}【${tip}】添加到回收站嘛？` : `确定要删除【${tip}】吗？`;
    Modal.confirm({
      title: '删除提示',
      content: tipStr,
      onOk: () => {
        deleteFunc().then(() => {
          this.$message.success(dustbin ? '已经添加到回收站!' : '删除成功!')
          resolve()
        }).catch((err: any) => {
          reject(err)
        })
      },
      onCancel() {
        reject()
      }
    });
  })
}

Vue.filter('DateFormat', function(date: string | number, format = 'YYYY/MM/DD HH-mm:ss') {
  if (date) {
    return timestamp(format, new Date(date))
  } else {
    return ''
  }
})

Vue.filter('ImageMfit', imageMfit)
Vue.filter('ImageLfit', imageLfit)
Vue.filter('ImageFill', imageFill)
Vue.filter('VideoFrame', videoFrame)
Vue.prototype.imageMfit = imageMfit
Vue.prototype.imageLfit = imageLfit
Vue.prototype.imageFill = imageFill
Vue.prototype.videoFrame = videoFrame

/// 注册全局组件
Vue.component('w-image', WImage)
